package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.act.ActConstant;
import com.motu.monstercity.server.game.commondata.act.ActDay;
import com.motu.monstercity.server.game.commondata.act.ActTask;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class ActDaysBaseData {
    private static Logger logger = LoggerFactory.getLogger(ActDaysBaseData.class);

    public static void initBaseData(List<Future> futures) {
        futures.add(initActDay());
        futures.add(initActTask());
    }

    private static ConcurrentHashMap<Integer, ActDay> actDayMap = new ConcurrentHashMap<>(); // 七天乐配置表
    private static ConcurrentHashMap<Integer, List<ActDay>> actDayGroupMap = new ConcurrentHashMap<>(); // 七天乐配置表

    /**
     * 初始化七天乐配置表
     */
    public static Future<Void> initActDay() {
        String table = "act_day";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + " order by group_id, day ASC;";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActDay> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ActDay>> tmpGroup = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActDay entity = new ActDay(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpGroup.computeIfAbsent(entity.getGroupId(), k -> new ArrayList<>()).add(entity);
                    }
                    actDayMap = tmp;
                    actDayGroupMap = tmpGroup;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActDay getActDay(int id) {
        ActDay result = actDayMap.get(id);
        if (result == null) {
            logger.error("getActDay error key = " + id);
        }
        return result;
    }
    public static List<ActDay> getActDaysByGroupId(int groupId) {
        return actDayGroupMap.get(groupId);
    }
    public static ActDay getActDaysByDay(int groupId, int day) {
        List<ActDay> result = getActDaysByGroupId(groupId);
        if (result == null || result.isEmpty()) {
            return null;
        }
        return result.stream().filter(d -> d.getDay() == day).findFirst().orElse(null);
    }
    public static ConcurrentHashMap<Integer, ActDay> getActDayMap() { return actDayMap; }

    private static ConcurrentHashMap<Integer, ActTask> actTaskMap = new ConcurrentHashMap<>(); // 七天乐任务配置表
    private static ConcurrentHashMap<Integer, List<ActTask>> actTaskGroupMap = new ConcurrentHashMap<>(); // 七天乐任务配置表
    private static ConcurrentHashMap<Integer, List<ActTask>> actTaskTypeMap = new ConcurrentHashMap<>(); // 七天乐任务配置表
    private static ConcurrentHashMap<Integer, List<ActTask>> actTaskActGroupMap = new ConcurrentHashMap<>(); // 七天乐任务配置表
    private static ConcurrentHashMap<Integer, List<ActTask>> actTaskActGroupMap2 = new ConcurrentHashMap<>(); //限时任务配置表

    /**
     * 初始化七天乐任务配置表
     */
    public static Future<Void> initActTask() {
        String table = "act_task";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ActTask> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ActTask>> tmpGroup = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ActTask>> tmpType = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ActTask>> tmpActGroup = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ActTask>> tmpActGroup2 = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ActTask entity = new ActTask(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpGroup.computeIfAbsent(entity.getTaskGroupId(), k -> new ArrayList<>()).add(entity);
                        tmpType.computeIfAbsent(entity.getTaskType(), k -> new ArrayList<>()).add(entity);
                        int actGroupId = entity.getTaskGroupId() / 10000;
                        tmpActGroup.computeIfAbsent(actGroupId, k -> new ArrayList<>()).add(entity);
                        tmpActGroup2.computeIfAbsent(entity.getTaskGroupId(), k -> new ArrayList<>()).add(entity);
                    }
                    actTaskMap = tmp;
                    actTaskGroupMap = tmpGroup;
                    actTaskTypeMap = tmpType;
                    actTaskActGroupMap = tmpActGroup;
                    actTaskActGroupMap2 = tmpActGroup2;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ActTask getActTask(int id) {
        ActTask result = actTaskMap.get(id);
        if (result == null) {
            logger.error("getActTask error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ActTask> getActTaskMap() { return actTaskMap; }
    public static ActTask getActTaskByType(int taskType) {
        List<ActTask> result = actTaskTypeMap.get(taskType);
        if (result == null || result.isEmpty()) {
            return null;
        }
        return result.stream().findFirst().orElse(null);
    }

    public static List<ActTask> getActTasksByGroupId(int groupId) {
        return actTaskGroupMap.get(groupId);
    }

    public static List<ActTask> getActTasksByGroupId(int groupId, int type) {
        List<ActTask> taskList = actTaskGroupMap.get(groupId);
        if (taskList == null || taskList.isEmpty()) {
            return null;
        }
        return taskList.stream().filter(t -> t.getTaskType() == type).toList();
    }
    public static List<ActTask> getActTasksByActGroupId(int groupId, int type) {
        List<ActTask> taskList = actTaskActGroupMap.get(groupId);
        if (taskList == null || taskList.isEmpty()) {
            return null;
        }
        return taskList.stream().filter(t -> t.getTaskType() == type).toList();
    }
    public static List<ActTask> getActTasksByActGroupId(int groupId, int type, int param) {
        List<ActTask> taskList = actTaskActGroupMap.get(groupId);
        if (taskList == null || taskList.isEmpty()) {
            logger.error("getActTasksByActGroupId error key = " + groupId + " type = " + type + " param = " + param);
            return null;
        }
        return taskList.stream().filter(t -> t.getTaskType() == type && t.getNum2() == param).toList();
    }

    public static List<ActTask> getRankActTasksByGroupId(int groupId, int type, int param) {
        List<ActTask> taskList = actTaskActGroupMap2.get(groupId);
        if (taskList == null || taskList.isEmpty()) {
            logger.error("getRankActTasksByGroupId error key = " + groupId + " type = " + type + " param = " + param);
            return null;
        }
        return taskList.stream().filter(t -> t.getTaskType() == type && t.getNum2() == param).toList();
    }

    public static List<ActTask> getActTaskByType(int groupId, int taskType, int param) {
        List<ActTask> result = new ArrayList<>();
        List<ActTask> actTaskList = actTaskGroupMap.get(groupId);
        if (actTaskList == null || actTaskList.isEmpty()) {
            return result;
        }
        for (ActTask actTask : actTaskList) {
            if (actTask.getTaskType() == taskType && actTask.getNum2() == param) {
                result.add(actTask);
            }
        }
        return result;
    }

    public static List<ActTask> getActTaskActGroupList(int actGroup) {
        return actTaskActGroupMap.get(actGroup);
    }

    public static ConcurrentHashMap<Integer, List<ActTask>> getActTaskActGroupMap() {
        return actTaskActGroupMap;
    }



    // 获取都市等级福利的配置
    public static ActTask getActTaskCity(int cityLevel) {
        for (ActTask actTask : actTaskMap.values()) {
            if (actTask.getTaskGroupId() == ActConstant.ACT_TASK_GROUP_ID_CITY && actTask.getNum1() == cityLevel) {
                return actTask;
            }
        }
        return null;
    }
}
